package com.youlian.cloud.core.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * swaager接口文档配置类
 * @author liuhx
 * @create 2017/08/01 17:11
 * @email liuhx@elab-plus.com
 **/
@Order(Integer.MAX_VALUE)
@EnableSwagger2
@Configuration
public class SwaggerConfigBean {

    @Autowired
    private Environment environment;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 具体需要扫描的包
                .apis(RequestHandlerSelectors.basePackage(environment.getProperty("swagger2.basePackage")))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(environment.getProperty("swagger2.title"))
                .description(environment.getProperty("swagger2.description"))
                .termsOfServiceUrl(environment.getProperty("swagger2.termsOfServiceUrl"))
                .version(environment.getProperty("swagger2.licenseUrl"))
                .licenseUrl(environment.getProperty("swagger2.version"))
                .build();
    }
}
